import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2024-11-14
 * Time: 8:42
 */
public class Main {

    //存在重复元素2
    public static boolean findtwo2(int[] nums,int k){
        //用map记录数字值和次数
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i])){
                map.put(nums[i],i);
            }else {
                int a=map.get(nums[i]);
                map.put(nums[i],i);
                int b=map.get(nums[i]);
                if(b-a<=k){
                    return true;
                }
            }
        }
        return false;
    }
    //存在重复元素1
    public static boolean findtwo(int[] nums){
        //用map记录数字值和次数
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i])){
                map.put(nums[i],1);
            }else {
                map.put(nums[i],2);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if(map.get(nums[i])==2){
                return true;
            }
        }
        return false;
    }
    //136、只出现一次的数字
    public static int findone(int[] nums){
        //用map记录数字值和次数
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i])){
                map.put(nums[i],1);
            }else {
                map.put(nums[i],2);
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if(map.get(nums[i])==1){
                return nums[i];
            }
        }
        return 0;

    }
    public static void main(String[] args) {
        int[] arr={1,2,3,1,2,3};
        System.out.println(findtwo2(arr,2));
    }
}